package m.jclecai.com.ui;

import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.activity.LottoTrendActivity;
import m.jclecai.com.activity.WebHelperActivity;
import m.jclecai.com.adapter.BallsAdapter;
import m.jclecai.com.adapter.SelectBoxAdapter;
import m.jclecai.com.adapter.independentLotteryAdapter.PLS_SD_QXCA_LotteryAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.BallItem;
import m.jclecai.com.bean.IssueInfoBean;
import m.jclecai.com.bean.MissDataBean;
import m.jclecai.com.constant.Constant_Result;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.contents.LotteryType;
import m.jclecai.com.inter.IAdapterChange;
import m.jclecai.com.inter.OnItemClick;
import m.jclecai.com.inter.OnResultCallback;
import m.jclecai.com.pop.SYXW_Center_PopupWindow;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.MakeRandomUtil;
import m.jclecai.com.utils.OkHttpClientManager;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.view.BallGridView;
import m.jclecai.com.view.common.BottomBar;
import m.jclecai.com.view.common.VeticalDrawerLayout;
import q.rorbin.badgeview.Badge;

/**
 * Created by hp on 2017/3/31.
 * <p>
 * 11选5 选号界面
 */

public class BetSEXWActivity extends BaseActivity implements View.OnClickListener,
        IAdapterChange, SensorEventListener, AdapterView.OnItemClickListener,
        RadioGroup.OnCheckedChangeListener {
    private String type;
    //提示
    private TextView tv_hint;

    private BallsAdapter itemOneAdapter, itemTwoAdapter, itemThreeAdapter;
    private LinearLayout ll_item_one, ll_item_two, ll_item_three;
    //位置标识
    private TextView tv_position_one, tv_position_two, tv_position_three;
    //选号控件
    private BallGridView gridview_item_one, gridview_item_two, gridview_item_three;
    //分割线
    private RelativeLayout line_one, line_two;
    //球总数
    private int ballCount;
    //传感器相关
    private SensorManager manager;
    private Vibrator vibrator;
    //注数金额
    private TextView tv_number_money;
    //截止时间
    private TextView tv_date;
    private int countOne, countTwo, countThree;

    private List<String> codeOne = new LinkedList<>();
    private List<String> codeTwo = new LinkedList<>();
    private List<String> codeThree = new LinkedList<>();

    private PopupWindow pop;
    private SYXW_Center_PopupWindow selectBoxPop;

    private GridView gv_selectBox;
    private SelectBoxAdapter adapter;
    private String[] ordinaryMode;
    private String[] ordinaryBonus;
    private String[] duplexMode;
    private String[] duplexBonus;
    private IssueInfoBean bean;
    //随机选号个数
    private int number;
    //是否是单选
    private boolean isRadio;
    //item个数
    private int itemCount;
    //玩法模式
    private String modes = "任选二";
    private CountDownTimer timer;
    private TextView tv_term;
    //普通玩法还是胆拖玩法
    private int currentPosition;
    private int checked;

    //最大胆码个数
    private int courageCount;

    private ArrayList<String> mode = new ArrayList<>();
    private ArrayList<String> numMoney = new ArrayList<>();
    private ArrayList<String> ballNumber = new ArrayList<>();
    private int totalMoney;
    private int totalNum;
    private ImageView iv_shake;
    private TextView tv_pull_hint;
    private TextView tv_courage_hint;

    //===========后来重新增加的部分===============
    private VeticalDrawerLayout mDrawerLayout;
    private ListView mLvHistory;
    private View mIvDateUnfold;
    private PLS_SD_QXCA_LotteryAdapter mhistoryAdapter;
    private BottomBar mBottomBar;

    //是否显示遗漏的textView;
    private TextView mTvMissData;

    @Override
    protected int getContentView() {
        return R.layout.betting_sexw_activity;
    }

    private int stringOfInt(String mode) {
        Log.i(TAG, "currentPosition=" + currentPosition);
        if (currentPosition == 0) {
            if (mode.equals("任选二")) {
                return 0;
            } else if (mode.equals("任选三")) {
                return 1;
            } else if (mode.equals("任选四")) {
                return 2;
            } else if (mode.equals("任选五")) {
                return 3;
            } else if (mode.equals("任选六")) {
                return 4;
            } else if (mode.equals("任选七")) {
                return 5;
            } else if (mode.equals("任选八")) {
                return 6;
            } else if (mode.equals("前一直选")) {
                return 7;
            } else if (mode.equals("前二直选")) {
                return 8;
            } else if (mode.equals("前三直选")) {
                return 9;
            } else if (mode.equals("前二组选")) {
                return 10;
            } else if (mode.equals("前三组选")) {
                return 11;
            }
        } else {
            if (mode.equals("任选二")) {
                return 12;
            } else if (mode.equals("任选三")) {
                return 13;
            } else if (mode.equals("任选四")) {
                return 14;
            } else if (mode.equals("任选五")) {
                return 15;
            } else if (mode.equals("任选六")) {
                return 16;
            } else if (mode.equals("任选七")) {
                return 17;
            } else if (mode.equals("前二组选")) {
                return 18;
            } else if (mode.equals("前三组选")) {
                return 19;
            }
        }
        return 99;
    }

    private void showView(boolean one, boolean two, boolean three) {
        clearBall();
        if (one) {
            ll_item_one.setVisibility(View.VISIBLE);
            line_one.setVisibility(View.VISIBLE);
        } else {
            ll_item_one.setVisibility(View.GONE);
            line_one.setVisibility(View.GONE);
        }

        if (two) {
            ll_item_two.setVisibility(View.VISIBLE);
            line_two.setVisibility(View.VISIBLE);
        } else {
            ll_item_two.setVisibility(View.GONE);
            line_two.setVisibility(View.GONE);
        }

        if (three) {
            ll_item_three.setVisibility(View.VISIBLE);
        } else {
            ll_item_three.setVisibility(View.GONE);
        }
    }


    private void initSelectBox() {

        selectBoxPop = new SYXW_Center_PopupWindow(this);
        selectBoxPop.setContent(R.layout.select_box_grid);
        selectBoxPop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        selectBoxPop.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //普通投注
        ordinaryMode = new String[]{"任选二", "任选三", "任选四", "任选五", "任选六", "任选七", "任选八", "前一直选", "前二直选", "前三直选", "前二组选", "前三组选"};
        ordinaryBonus = new String[]{"奖金7元", "奖金25元", "奖金115元", "奖金930元", "奖金115元", "奖金44元", "奖金16元", "奖金14元", "奖金155元", "奖金1550元", "奖金77元", "奖金255元"};

        //胆拖投注
        duplexMode = new String[]{"任选二", "任选三", "任选四", "任选五", "任选六", "任选七", "前二组选", "前三组选"};
        duplexBonus = new String[]{"奖金7元", "奖金25元", "奖金115元", "奖金930元", "奖金115元", "奖金44元", "奖金77元", "奖金255元"};

        View view = selectBoxPop.getView();
        gv_selectBox = (GridView) view.findViewById(R.id.gv_selectBox);

        RadioGroup rg_tab = (RadioGroup) view.findViewById(R.id.rg_tab);
        rg_tab.setOnCheckedChangeListener(this);

        int w = BaseUtils.getDisplaySize(this, "width");
        int width = w / 3;
        gv_selectBox.setColumnWidth(width);
        adapter = new SelectBoxAdapter(this, new OnItemClick() {
            @Override
            public void onItemClick(String mode) {
                if (checked == 0) {
                    Log.i(TAG, "普通玩法");
                    currentPosition = 0;
                    iv_shake.setVisibility(View.VISIBLE);
                    manager.registerListener(BetSEXWActivity.this, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
                } else if (checked == 1) {
                    Log.i(TAG, "胆拖玩法");
                    currentPosition = 1;
                    iv_shake.setVisibility(View.GONE);
                    manager.unregisterListener(BetSEXWActivity.this);
                }
                modes = mode;

                setTitle("浙江十二选五-" + mode);
                int i = stringOfInt(mode);
                Log.i(TAG, "mItemClickeListener=" + i);
                switch (i) {
                    case 0:
                        showView(true, false, false);
                        tv_hint.setText(Html.fromHtml("至少选2个号，猜中任意2个开奖号，奖金 <font color=red>7</font>元"));
                        tv_courage_hint.setVisibility(View.GONE);
                        tv_pull_hint.setVisibility(View.GONE);
                        tv_position_one.setText("选号");
                        number = 2;
                        isRadio = false;
                        break;
                    case 1:
                        showView(true, false, false);
                        tv_hint.setText(Html.fromHtml("至少选3个号，猜中任意3个开奖号，奖金 <font color=red>25</font>元"));
                        tv_courage_hint.setVisibility(View.GONE);
                        tv_pull_hint.setVisibility(View.GONE);
                        tv_position_one.setText("选号");
                        number = 3;
                        isRadio = false;
                        break;
                    case 2:
                        showView(true, false, false);
                        tv_hint.setText(Html.fromHtml("至少选4个号，猜中任意4个开奖号，奖金 <font color=red>115</font>元"));
                        tv_courage_hint.setVisibility(View.GONE);
                        tv_pull_hint.setVisibility(View.GONE);
                        tv_position_one.setText("选号");
                        number = 4;
                        isRadio = false;
                        break;
                    case 3:
                        showView(true, false, false);
                        tv_hint.setText(Html.fromHtml("至少选5个号，猜中全部5个开奖号，奖金 <font color=red>930</font>元"));
                        tv_courage_hint.setVisibility(View.GONE);
                        tv_pull_hint.setVisibility(View.GONE);
                        tv_position_one.setText("选号");
                        number = 5;
                        isRadio = false;
                        break;
                    case 4:
                        showView(true, false, false);
                        tv_hint.setText(Html.fromHtml("至少选6个号，猜中任意5个开奖号，奖金 <font color=red>115</font>元"));
                        tv_courage_hint.setVisibility(View.GONE);
                        tv_pull_hint.setVisibility(View.GONE);
                        tv_position_one.setText("选号");
                        number = 6;
                        isRadio = false;
                        break;
                    case 5:
                        showView(true, false, false);
                        tv_hint.setText(Html.fromHtml("至少选7个号，猜中任意5个开奖号，奖金 <font color=red>44</font>元"));
                        tv_courage_hint.setVisibility(View.GONE);
                        tv_pull_hint.setVisibility(View.GONE);
                        tv_position_one.setText("选号");
                        number = 7;
                        isRadio = false;
                        break;
                    case 6:
                        showView(true, false, false);
                        tv_hint.setText(Html.fromHtml("至少选8个号，猜中任意5个开奖号，奖金 <font color=red>16</font>元"));
                        tv_courage_hint.setVisibility(View.GONE);
                        tv_pull_hint.setVisibility(View.GONE);
                        tv_position_one.setText("选号");
                        number = 8;
                        isRadio = false;
                        break;
                    case 7:
                        showView(true, false, false);
                        tv_hint.setText(Html.fromHtml("至少选1个号，与开奖号首位相同，奖金 <font color=red>14</font>元"));
                        tv_courage_hint.setVisibility(View.GONE);
                        tv_pull_hint.setVisibility(View.GONE);
                        tv_position_one.setText("选号");
                        number = 1;
                        itemCount = 1;
                        isRadio = true;

                        break;
                    case 8:
                        showView(true, true, false);
                        tv_hint.setText(Html.fromHtml("每位至少选1个号，按位猜对开奖号前2位，奖金 <font color=red>155</font>元"));
                        tv_courage_hint.setVisibility(View.GONE);
                        tv_pull_hint.setVisibility(View.GONE);
                        tv_position_one.setText("前一");
                        tv_position_two.setText("前二");
                        number = 1;
                        itemCount = 2;
                        isRadio = true;
                        break;
                    case 9:
                        showView(true, true, true);
                        tv_hint.setText(Html.fromHtml("每位至少选1个号，按位猜对开奖号前3位，奖金 <font color=red>1550</font>元"));
                        tv_courage_hint.setVisibility(View.GONE);
                        tv_pull_hint.setVisibility(View.GONE);
                        tv_position_one.setText("前一");
                        tv_position_two.setText("前二");
                        tv_position_three.setText("前三");
                        number = 1;
                        itemCount = 3;
                        isRadio = true;
                        break;
                    case 10:
                        showView(true, false, false);
                        tv_hint.setText(Html.fromHtml("至少选2个号，猜中开奖号前2位，奖金 <font color=red>77</font>元"));
                        tv_courage_hint.setVisibility(View.GONE);
                        tv_pull_hint.setVisibility(View.GONE);
                        tv_position_one.setText("选号");
                        number = 2;
                        itemCount = 1;
                        isRadio = false;
                        break;
                    case 11:
                        showView(true, false, false);
                        tv_hint.setText(Html.fromHtml("至少选3个号，猜中开奖号前3位，奖金 <font color=red>255</font>元"));
                        tv_courage_hint.setVisibility(View.GONE);
                        tv_pull_hint.setVisibility(View.GONE);
                        tv_position_one.setText("选号");
                        number = 3;
                        itemCount = 1;
                        isRadio = false;
                        break;
                    case 12:
                        showView(true, true, false);
                        tv_hint.setText(Html.fromHtml("至少选3个号，猜中任意2个开奖号，奖金 <font color=red>7</font>元"));
                        tv_courage_hint.setVisibility(View.VISIBLE);
                        tv_courage_hint.setText("我认为必出的号码(选择1个)");
                        tv_pull_hint.setVisibility(View.VISIBLE);
                        tv_pull_hint.setText("我认为可能出的号码(与胆码相加不少于3个)");
                        tv_position_one.setText("胆码");
                        tv_position_two.setText("托码");
                        number = 3;
                        itemCount = 2;
                        courageCount = 1;
                        isRadio = false;
                        break;
                    case 13:
                        showView(true, true, false);
                        tv_hint.setText(Html.fromHtml("至少选4个号，猜中任意3个开奖号，奖金 <font color=red>25</font>元"));
                        tv_courage_hint.setVisibility(View.VISIBLE);
                        tv_courage_hint.setText("我认为必出的号码(选择1-2个)");
                        tv_pull_hint.setVisibility(View.VISIBLE);
                        tv_pull_hint.setText("我认为可能出的号码(与胆码相加不少于4个)");
                        tv_position_one.setText("胆码");
                        tv_position_two.setText("托码");
                        number = 4;
                        itemCount = 2;
                        courageCount = 2;
                        isRadio = false;
                        break;
                    case 14:
                        showView(true, true, false);
                        tv_hint.setText(Html.fromHtml("至少选5个号，猜中任意4个开奖号，奖金 <font color=red>115</font>元"));
                        tv_courage_hint.setVisibility(View.VISIBLE);
                        tv_courage_hint.setText("我认为必出的号码(选择1-3个)");
                        tv_pull_hint.setVisibility(View.VISIBLE);
                        tv_pull_hint.setText("我认为可能出的号码(与胆码相加不少于5个)");
                        tv_position_one.setText("胆码");
                        tv_position_two.setText("托码");
                        number = 5;
                        itemCount = 2;
                        courageCount = 3;
                        isRadio = false;
                        break;
                    case 15:
                        showView(true, true, false);
                        tv_hint.setText(Html.fromHtml("至少选6个号，猜中任意4个开奖号，奖金 <font color=red>930</font>元"));
                        tv_courage_hint.setVisibility(View.VISIBLE);
                        tv_courage_hint.setText("我认为必出的号码(选择1-4个)");
                        tv_pull_hint.setVisibility(View.VISIBLE);
                        tv_pull_hint.setText("我认为可能出的号码(与胆码相加不少于6个)");
                        tv_position_one.setText("胆码");
                        tv_position_two.setText("托码");
                        number = 6;
                        itemCount = 2;
                        courageCount = 4;
                        isRadio = false;
                        break;
                    case 16:
                        showView(true, true, false);
                        tv_hint.setText(Html.fromHtml("至少选7个号，猜中任意6个开奖号，奖金 <font color=red>115</font>元"));
                        tv_courage_hint.setVisibility(View.VISIBLE);
                        tv_courage_hint.setText("我认为必出的号码(选择1-5个)");
                        tv_pull_hint.setVisibility(View.VISIBLE);
                        tv_pull_hint.setText("我认为可能出的号码(与胆码相加不少于7个)");
                        tv_position_one.setText("胆码");
                        tv_position_two.setText("托码");
                        number = 7;
                        itemCount = 2;
                        courageCount = 5;
                        isRadio = false;
                        break;
                    case 17:
                        showView(true, true, false);
                        tv_hint.setText(Html.fromHtml("至少选8个号，猜中全部5个开奖号，奖金 <font color=red>44</font>元"));
                        tv_courage_hint.setVisibility(View.VISIBLE);
                        tv_courage_hint.setText("我认为必出的号码(选择1-6个)");
                        tv_pull_hint.setVisibility(View.VISIBLE);
                        tv_pull_hint.setText("我认为可能出的号码(与胆码相加不少于8个)");
                        tv_position_one.setText("胆码");
                        tv_position_two.setText("托码");
                        number = 8;
                        itemCount = 2;
                        courageCount = 6;
                        isRadio = false;
                        break;
                    case 18:
                        showView(true, true, false);
                        tv_hint.setText(Html.fromHtml("至少选3个号，猜中开奖号前2位，奖金 <font color=red>77</font>元"));
                        tv_courage_hint.setVisibility(View.VISIBLE);
                        tv_courage_hint.setText("我认为必出的号码(选择1个)");
                        tv_pull_hint.setVisibility(View.VISIBLE);
                        tv_pull_hint.setText("我认为可能出的号码(与胆码相加不少于3个)");
                        tv_position_one.setText("胆码");
                        tv_position_two.setText("托码");
                        number = 3;
                        itemCount = 2;
                        courageCount = 2;
                        isRadio = false;
                        break;
                    case 19:
                        showView(true, true, false);
                        tv_hint.setText(Html.fromHtml("至少选4个号，猜中开奖号前3位，奖金 <font color=red>255</font>元"));
                        tv_courage_hint.setVisibility(View.VISIBLE);
                        tv_courage_hint.setText("我认为必出的号码(选择1-2个)");
                        tv_pull_hint.setVisibility(View.VISIBLE);
                        tv_pull_hint.setText("我认为可能出的号码(与胆码相加不少于4个)");
                        tv_position_one.setText("胆码");
                        tv_position_two.setText("托码");
                        number = 4;
                        itemCount = 2;
                        courageCount = 3;
                        isRadio = false;
                        break;
                }
                selectBoxPop.dismiss();
            }
        });

        //       selectBoxPop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        selectBoxPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                startAnimation(ivTitleIcon, R.anim.rotate_180_360);
            }
        });

        ((RadioButton) rg_tab.getChildAt(0)).setChecked(true);
        adapter.setData(ordinaryMode, ordinaryBonus);
        gv_selectBox.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void initView() {
        leftOptionText("");
        rightOptionText("", R.drawable.action_menu);

        iv_shake = (ImageView) findViewById(R.id.iv_shake);
        iv_shake.setOnClickListener(this);
        tv_hint = (TextView) findViewById(R.id.tv_hint);
        tv_number_money = (TextView) findViewById(R.id.tv_number_money);

        tv_term = (TextView) findViewById(R.id.tv_term);
        tv_date = (TextView) findViewById(R.id.tv_date);


        mDrawerLayout = ((VeticalDrawerLayout) findViewById(R.id.drawer_layout));
        mLvHistory = ((ListView) findViewById(R.id.lv_history));
        mLvHistory.setDivider(null);
        mDrawerLayout = ((VeticalDrawerLayout) findViewById(R.id.drawer_layout));
        mIvDateUnfold = findViewById(R.id.iv_date_unfold);

        mDrawerLayout.setOnDragStatusChange(new VeticalDrawerLayout.OnDragStatusChange() {
            @Override
            public void onOpen() {

            }

            @Override
            public void onClose() {

            }

            @Override
            public void OnDrag(float percent) {
                if (percent >= 0 && percent <= 1) {
                    Float evaluate = BaseUtils.evaluate(percent, 0, 180);
                    mIvDateUnfold.setRotation(evaluate);
                }
            }
        });
        mIvDateUnfold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.toggle();
            }
        });

        //这里要做的一个逻辑就是；
        mBottomBar = ((BottomBar) findViewById(R.id.bottom_bar));
        mBottomBar.setLeftButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = (TextView) v;
                String text = textView.getText().toString();
                if ("机选".equals(text)) {
                    //随机选中一个
                    randomSelect();
                    textView.setText("清空");
                } else {
                    //清空逻辑；
                    clearBall();
                    textView.setText("机选");
                }
            }
        });

        mBottomBar.setCenterButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                restrict = false;
                duplexRestrict = false;
                isTo = false;

                if (currentPosition == 0) {
                    restrict = true;
                } else {
                    duplexRestrict = true;
                }
                addAllToBetting();
            }
        });
        mBottomBar.setRightButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //下一步的按钮；

                if ("未开售".equals(tv_term.getText().toString())) {
                    //这里的话，我们要判断一下是否开售了，如果没有的话，我们是不允许跳转的
                    DisPlay("未开售！！！");
                    return;
                }

                restrict = false;
                duplexRestrict = false;
                isTo = false;

                isTo = true;
                if (currentPosition == 0) {
                    if (modes.equals("前二直选")) {
                        if (ballNumber.size() > 0 && codeOne.size() == 0 && codeTwo.size() == 0) {
                            toActivity();
                        } else if (ballNumber.size() == 0 && codeOne.size() == 0 && codeTwo.size() == 0) {
                            randomSelect();
                        } else {
                            restrict = true;
                        }
                    } else if (modes.equals("前三直选")) {
                        if (ballNumber.size() > 0 && codeOne.size() == 0 && codeTwo.size() == 0 && codeThree.size() == 0) {
                            toActivity();
                        } else if (ballNumber.size() == 0 && codeOne.size() == 0 && codeTwo.size() == 0 && codeThree.size() == 0) {
                            randomSelect();
                        } else {
                            restrict = true;
                        }
                    } else { //非直选
                        if (ballNumber.size() > 0 && codeOne.size() == 0) {
                            toActivity();
                        } else if (ballNumber.size() == 0 && codeOne.size() == 0) {
                            randomSelect();
                        } else {
                            restrict = true;
                        }
                    }
                } else {
                    //胆拖投注
                    //点击下一步判断ballNumber是否有数据，如果有则直接跳入，如果没有则对现有的选择进行判断
                    if (ballNumber.size() > 0 && codeOne.size() == 0 && codeTwo.size() == 0) {
                        toActivity();
                    } else if (ballNumber.size() == 0 && codeOne.size() < courageCount && (codeOne.size() + codeTwo.size() < number)) {
                        ToastUtil.showToast(BetSEXWActivity.this, "至少选择" + number + "个号码(胆码+托码)");
                    } else {
                        duplexRestrict = true;
                    }
                }
                addAllToBetting();
            }
        });
        mBottomBar.setOnDragstateChangeListener(new Badge.OnDragStateChangedListener() {
            @Override
            public void onDragStateChanged(int dragState, Badge badge, View targetView) {
                if (dragState == Badge.OnDragStateChangedListener.STATE_SUCCEED) {
                    //清空投注区；
                    clearAllBetting();
                }
            }
        });

        initBallView();
    }

    private void addAllToBetting() {
        //===============检查
        //胆拖投注检查
        if (duplexRestrict) {
            if (codeOne.size() <= courageCount && (codeOne.size() + codeTwo.size() >= number)) {
                addSelector(isTo);
            }
        }
        //普通投注检查
        if (restrict) {
            if (modes.equals("任选二") && codeOne.size() >= 2) {
                addSelector(isTo);
            } else if (modes.equals("任选三") && codeOne.size() >= 3) {
                addSelector(isTo);
            } else if (modes.equals("任选四") && codeOne.size() >= 4) {
                addSelector(isTo);
            } else if (modes.equals("任选五") && codeOne.size() >= 5) {
                addSelector(isTo);
            } else if (modes.equals("任选六") && codeOne.size() >= 6) {
                addSelector(isTo);
            } else if (modes.equals("任选七") && codeOne.size() >= 7) {
                addSelector(isTo);
            } else if (modes.equals("任选八") && codeOne.size() >= 8) {
                addSelector(isTo);
            } else if (modes.equals("前一直选") && codeOne.size() >= 1) {
                addSelector(isTo);
            } else if (modes.equals("前二直选") && codeOne.size() >= 1 && codeTwo.size() >= 1) {
                addSelector(isTo);
            } else if (modes.equals("前三直选") && codeOne.size() >= 1 && codeTwo.size() >= 1 && codeThree.size() >= 1) {
                addSelector(isTo);
            } else if (modes.equals("前二组选") && codeOne.size() >= 2) {
                addSelector(isTo);
            } else if (modes.equals("前三组选") && codeOne.size() >= 3) {
                addSelector(isTo);
            }
        }
    }

    private void initBallView() {
        tv_position_one = (TextView) findViewById(R.id.tv_position_one);
        tv_position_two = (TextView) findViewById(R.id.tv_position_two);
        tv_position_three = (TextView) findViewById(R.id.tv_position_three);

        ll_item_one = (LinearLayout) findViewById(R.id.ll_item_one);
        ll_item_two = (LinearLayout) findViewById(R.id.ll_item_two);
        ll_item_three = (LinearLayout) findViewById(R.id.ll_item_three);

        line_one = (RelativeLayout) findViewById(R.id.line_one);
        line_two = (RelativeLayout) findViewById(R.id.line_two);

        gridview_item_one = (BallGridView) findViewById(R.id.gridview_item_one);
        itemOneAdapter = new BallsAdapter(this);
        gridview_item_one.setOnItemClickListener(this);
        gridview_item_one.setNumColumns(6);

        gridview_item_two = (BallGridView) findViewById(R.id.gridview_item_two);
        itemTwoAdapter = new BallsAdapter(this);
        gridview_item_two.setOnItemClickListener(this);
        gridview_item_two.setNumColumns(6);

        gridview_item_three = (BallGridView) findViewById(R.id.gridview_item_three);
        itemThreeAdapter = new BallsAdapter(this);
        gridview_item_three.setOnItemClickListener(this);
        gridview_item_three.setNumColumns(6);

        tv_courage_hint = (TextView) findViewById(R.id.tv_courage_hint);
        tv_pull_hint = (TextView) findViewById(R.id.tv_pull_hint);
    }

    private void initPopupView() {
        View pv = View.inflate(this, R.layout.lottery_help_popupview, null);
        pop = new PopupWindow(pv, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        TextView tv_trendChart = (TextView) pv.findViewById(R.id.tv_trendChart);
        tv_trendChart.setOnClickListener(this);
        TextView tv_lotteryHistory = (TextView) pv.findViewById(R.id.tv_kaijiang);
        tv_lotteryHistory.setOnClickListener(this);

        pv.findViewById(R.id.tv_play_desc).setOnClickListener(this);
        //这里我们要设置显示遗漏的地方；

        mTvMissData = (TextView) pv.findViewById(R.id.tv_missData);
        mTvMissData.setOnClickListener(this);

        boolean showMissing = PreferenceUtil.isShowMissing(this);
        if (showMissing) {
            //这里表示显示；
            showMissData();
        } else {
            hideMissData();
        }


        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
    }

    @Override
    protected void initData() {
        manager = (SensorManager) getSystemService(SENSOR_SERVICE);
        vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);

        Intent intent = getIntent();
        type = intent.getStringExtra("type");

        setTitle("浙江十二选五-任选二", R.drawable.gs);
        tv_hint.setText("至少选2个号，猜中任意2个开奖号，奖金7元");
        tv_position_one.setText("选号");

        ll_item_one.setVisibility(View.VISIBLE);
        ballCount = 12;
        number = 2;

        gridview_item_one.setAdapter(itemOneAdapter);
        gridview_item_two.setAdapter(itemTwoAdapter);
        gridview_item_three.setAdapter(itemThreeAdapter);

        //注册震动
        manager.registerListener(this, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);


        initBallList();
        getMissData(type);
        getIssueInfo(type);
        initPopupView();
        initSelectBox();
    }

    private void getMissData(String type) {
        //showDialog();
        OkHttpClientManager.Param Url = new OkHttpClientManager.Param("url", Constant_Url.GET_MISS_DATA);
        OkHttpClientManager.Param Type = new OkHttpClientManager.Param("type", type);
        OkHttpClientManager.Param[] param = new OkHttpClientManager.Param[]{Url, Type};
        try {
            OkHttpClientManager.getInstance().asynPost(new OnResultCallback() {
                @Override
                public void onGetResult(String json) {
                    MissDataBean bean = FastOk.get(json, MissDataBean.class);

                    setMissData(bean);
                    // dissDialog();
                }

                @Override
                public void onError() {
                    ToastUtil.showToast(BetSEXWActivity.this, R.string.connect_failure);
                    //dissDialog();
                }
            }, param);
        } catch (IOException e) {
            e.printStackTrace();
            dissDialog();
        }
    }

    private void setMissData(MissDataBean missData) {
        //这里的话，我们是需要设置遗漏和最近10期的开奖情况；
        mhistoryAdapter = new PLS_SD_QXCA_LotteryAdapter(this);
        mhistoryAdapter.setData(missData.getCodes());
        mLvHistory.setAdapter(mhistoryAdapter);
        //这里的话，我们要做的一个操作就是测量

        int height = measureListViewWrongHeight(mLvHistory);

        mLvHistory.getLayoutParams().height = height;
        mLvHistory.requestLayout();
        mDrawerLayout.measureInnerLayout();
        if (missData.getOmission() != null) {
            //这里是设置遗漏
            itemOneAdapter.addMissDataInfo(missData.getOmission().getM0());
            itemTwoAdapter.addMissDataInfo(missData.getOmission().getM1());
            itemThreeAdapter.addMissDataInfo(missData.getOmission().getM2());
        }
        itemOneAdapter.notifyDataSetChanged();
        itemTwoAdapter.notifyDataSetChanged();
        itemThreeAdapter.notifyDataSetChanged();

    }

    private void getIssueInfo(String type) {
        OkHttpClientManager.Param Url = new OkHttpClientManager.Param("url", Constant_Url.ISSUE_INFO);
        OkHttpClientManager.Param Type = new OkHttpClientManager.Param("type", type);
        OkHttpClientManager.Param[] param = new OkHttpClientManager.Param[]{Url, Type};
        try {
            OkHttpClientManager.getInstance().asynPost(new OnResultCallback() {
                @Override
                public void onGetResult(String json) {
                    IssueInfoBean bean = FastOk.get(json, IssueInfoBean.class);
                    setViewInfo(bean);
                }

                @Override
                public void onError() {
                    ToastUtil.showToast(BetSEXWActivity.this, R.string.connect_failure);
                }
            }, param);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void setViewInfo(IssueInfoBean bean) {
        if (bean == null) return;
        try {
            this.bean = bean;
            String term = bean.getTermNo();
            //获取当前时间
            String currentTime = BaseUtils.getCurrentTime();
            //获取截止投注时间和当前时间的差值
            long m = BaseUtils.contrastDate(bean.getStopSaleTime(), currentTime, 2);
            long s = BaseUtils.contrastDate(bean.getStopSaleTime(), currentTime, 3);
            long time = m * 60 + s;
            String str = term.substring(term.length() - 2, term.length());
            tv_term.setText("距" + str + "期截止");
            startCountDownTime(Long.parseLong(bean.getDeadLine()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void startCountDownTime(final long time) {
        timer = new CountDownTimer(time * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

                tv_date.setText(BaseUtils.formatDateLineToString(millisUntilFinished));
            }

            @Override
            public void onFinish() {
                tv_date.setText("已截止");
                getIssueInfo(type);
            }
        };
        timer.start();// 开始计时
    }

    private void initBallList() {

        //每个位置球数
        int ones = 1;

        int twos = 1;

        int threes = 1;
        //球item
        BallItem ballItem = null;


        while (true) {
            if (ones > ballCount) {
                break;
            }
            if (ones < 10) {
                ballItem = new BallItem("0" + ones, false, true);
            } else {
                ballItem = new BallItem("" + ones, false, true);
            }
            itemOneAdapter.addItem(ballItem);
            ones++;
        }


        while (true) {
            if (twos > ballCount) {
                break;
            }
            if (twos < 10) {
                ballItem = new BallItem("0" + twos, false, true);
            } else {
                ballItem = new BallItem("" + twos, false, true);
            }
            itemTwoAdapter.addItem(ballItem);
            twos++;
        }

        while (true) {
            if (threes > ballCount) {
                break;
            }
            if (threes < 10) {
                ballItem = new BallItem("0" + threes, false, true);
            } else {
                ballItem = new BallItem("" + threes, false, true);
            }
            itemThreeAdapter.addItem(ballItem);
            threes++;
        }

        itemOneAdapter.notifyDataSetChanged();
        itemTwoAdapter.notifyDataSetChanged();
        itemThreeAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.tv_mid_title, R.id.iv_title_icon})
    void showLotteryMenu() {
        startAnimation(ivTitleIcon, R.anim.rotate_0_180);
        height = rlBar.getHeight();
        statusHeight = BaseUtils.getStatusHeight(this);
        //selectBoxPop.showAtLocation(rlBar, 0, 0, height + statusHeight);
        selectBoxPop.showAsDropDown(rlBar);
    }

    @OnClick(R.id.tv_right_option)
    void showHelpMenu() {
        height = rlBar.getHeight();
        statusHeight = BaseUtils.getStatusHeight(this);
        width = BaseUtils.getDisplaySize(this, "width");
        pop.showAtLocation(rlBar, 0, width, height + statusHeight);
    }

    int height, statusHeight, width;

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.iv_shake:
                randomSelect();
                break;
            case R.id.bt_bottom_left:
                clearBall();
                break;
            case R.id.tv_trendChart:
                pop.dismiss();
                intent = new Intent(this, LottoTrendActivity.class);
                intent.putExtra("lotteryId", LotteryType.ZJSEXW);
                intent.putExtra("title", "浙江12选5");
                startActivity(intent);
                break;
            case R.id.tv_kaijiang:
                pop.dismiss();
                intent = new Intent(this, LotteryHistoryActivity.class);
                intent.putExtra("type", type);
                startActivity(intent);
                break;
            case R.id.tv_missData:
                boolean showMissing = PreferenceUtil.isShowMissing(BetSEXWActivity.this);
                if (showMissing) {
                    hideMissData();
                } else {
                    showMissData();
                }
                pop.dismiss();
                break;
            case R.id.tv_play_desc:
                Intent helpIntent = new Intent(this, WebHelperActivity.class);
                helpIntent.putExtra("title", "玩法介绍");
                helpIntent.putExtra("code", type);
                helpIntent.putExtra("isLocal", true);
                startActivity(helpIntent);
                break;
        }
    }

    @Override
    public View onItemChanged(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        BallItem ballItem = null;
        boolean selected = false;
        String num = null;
        int mViewGroup = 0;

        BallsAdapter ballsAdapter = null;

        if (parent.getId() == R.id.gridview_item_one) {
            ballItem = (BallItem) itemOneAdapter.getItem(position);
            mViewGroup = R.layout.ballgridview_ball_item;

            ballsAdapter = itemOneAdapter;
        } else if (parent.getId() == R.id.gridview_item_two) {
            ballItem = (BallItem) itemTwoAdapter.getItem(position);
            mViewGroup = R.layout.ballgridview_ball_item;

            ballsAdapter = itemTwoAdapter;
        } else if (parent.getId() == R.id.gridview_item_three) {
            ballItem = (BallItem) itemThreeAdapter.getItem(position);
            mViewGroup = R.layout.ballgridview_ball_item;

            ballsAdapter = itemThreeAdapter;
        }

        if (null == convertView) {
            holder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(this);
            convertView = inflater.inflate(mViewGroup, parent, false);
            holder.ball_num = (TextView) convertView.findViewById(R.id.txt_ball_num);
            holder.tv_missData = (TextView) convertView.findViewById(R.id.tv_missData);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        selected = ballItem.isSelected();
        num = ballItem.getBallNum();


        holder.ball_num.setText(num);

        if (ballsAdapter.getMissDataInfo().length > 0) {
            holder.tv_missData.setText(ballsAdapter.getMissDataInfo()[position]);
        } else {
            holder.tv_missData.setText("-");
        }

        if (ballsAdapter.isShowMissData()) {
            holder.tv_missData.setVisibility(View.VISIBLE);
        } else {
            holder.tv_missData.setVisibility(View.GONE);
        }

        //判断球是否被选中
        if (selected) {
            holder.ball_num.setBackgroundResource(R.drawable.red_ball);
            holder.ball_num.setTextColor(Color.WHITE);
        } else {
            holder.ball_num.setBackgroundResource(R.drawable.shape_ball_red_bg);
            holder.ball_num.setTextColor(getResources().getColor(R.color.baseRed));
        }
        return convertView;
    }


    /**
     * 胆拖玩法下，胆码和托码不能相同，胆码有规定选择个数限制
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        boolean oneIsSelect = false;
        boolean twoIsSelect = false;
        boolean threeIsSelect = false;

        BallItem ballItemOne = (BallItem) itemOneAdapter.getItem(position);
        BallItem ballItemTwo = (BallItem) itemTwoAdapter.getItem(position);
        BallItem ballItemThree = (BallItem) itemThreeAdapter.getItem(position);

        if (parent.getId() == R.id.gridview_item_one) {
            //普通玩法
            if (currentPosition == 0) {
                //判断是否是直选
                if (isRadio) {
                    if (codeTwo.size() > 0) {
                        for (int i = 0; i < codeTwo.size(); i++) {
                            //item2选中和item1选中的作比较,相同则去除item2添加itme1
                            if (ballItemOne.getBallNum().equals(codeTwo.get(i))) {
                                ballItemTwo.setSelected(false);
                                countTwo--;
                                codeTwo.remove(ballItemTwo.getBallNum());

                                ballItemOne.setSelected(true);
                                countOne++;
                                codeOne.add(ballItemOne.getBallNum());
                                break;
                            } else {
                                if (i == codeTwo.size() - 1) {
                                    twoIsSelect = true;
                                }
                            }
                        }
                    } else {
                        twoIsSelect = true;
                    }
                    if (codeThree.size() > 0) {
                        for (int i = 0; i < codeThree.size(); i++) {
                            if (ballItemOne.getBallNum().equals(codeThree.get(i))) {
                                ballItemThree.setSelected(false);
                                countThree--;
                                codeThree.remove(ballItemThree.getBallNum());
                                ballItemOne.setSelected(true);
                                countOne++;
                                codeOne.add(ballItemOne.getBallNum());
                                break;
                            } else {
                                if (i == codeThree.size() - 1) {
                                    threeIsSelect = true;
                                }
                            }
                        }
                    } else {
                        threeIsSelect = true;
                    }

                    //item3和item2中都没有和点击匹配的
                    if (twoIsSelect && threeIsSelect) {
                        if (ballItemOne.isSelected()) {
                            ballItemOne.setSelected(false);
                            countOne--;
                            codeOne.remove(ballItemOne.getBallNum());
                        } else {
                            ballItemOne.setSelected(true);
                            countOne++;
                            codeOne.add(ballItemOne.getBallNum());
                        }
                    }

                    setNumberMoney(countOne, countTwo, countThree);
                    itemOneAdapter.notifyDataSetChanged();
                    itemTwoAdapter.notifyDataSetChanged();
                    itemThreeAdapter.notifyDataSetChanged();
                    //非单选
                } else {
                    if (ballItemOne.isSelected()) {
                        ballItemOne.setSelected(false);
                        countOne--;
                        codeOne.remove(ballItemOne.getBallNum());
                    } else {
                        ballItemOne.setSelected(true);
                        countOne++;
                        codeOne.add(ballItemOne.getBallNum());
                    }
                    setNumberMoney(countOne, countTwo, countThree);
                    itemOneAdapter.notifyDataSetChanged();
                }
                //胆拖玩法
            } else {
                if (ballItemOne.isSelected()) {
                    ballItemOne.setSelected(false);
                    countOne--;
                    codeOne.remove(ballItemOne.getBallNum());
                } else {
                    //选中的号码不能大于最大胆码数
                    if (countOne < courageCount) {
                        ballItemOne.setSelected(true);
                        countOne++;
                        codeOne.add(ballItemOne.getBallNum());
                    } else {
                        ToastUtil.showToast(this, "最多只能选择" + courageCount + "个胆码");
                    }
                }
                setNumberMoney(countOne, countTwo, countThree);
                itemOneAdapter.notifyDataSetChanged();
            }
        } else if (parent.getId() == R.id.gridview_item_two) {

            if (currentPosition == 0) {
                if (isRadio) {
                    if (codeOne.size() > 0) {
                        for (int i = 0; i < codeOne.size(); i++) {
                            if (ballItemTwo.getBallNum().equals(codeOne.get(i))) {
                                ballItemOne.setSelected(false);
                                countOne--;
                                codeOne.remove(ballItemOne.getBallNum());

                                ballItemTwo.setSelected(true);
                                countTwo++;
                                codeTwo.add(ballItemTwo.getBallNum());
                                break;
                            } else { //点击的item2和item1不匹配
                                if (i == codeOne.size() - 1) {
                                    oneIsSelect = true;
                                }
                            }
                        }
                    } else { //item1中没有选中的
                        oneIsSelect = true;
                    }

                    if (codeThree.size() > 0) {
                        for (int i = 0; i < codeThree.size(); i++) {
                            if (ballItemTwo.getBallNum().equals(codeThree.get(i))) {
                                ballItemThree.setSelected(false);
                                countThree--;
                                codeThree.remove(ballItemThree.getBallNum());

                                ballItemTwo.setSelected(true);
                                countTwo++;
                                codeTwo.add(ballItemTwo.getBallNum());
                                break;
                            } else { //item3中没有和item2匹配的
                                if (i == codeThree.size() - 1) {
                                    threeIsSelect = true;
                                }
                            }
                        }
                    } else { //item3中没有选中
                        threeIsSelect = true;
                    }

                    if (oneIsSelect && threeIsSelect) {
                        if (ballItemTwo.isSelected()) {
                            ballItemTwo.setSelected(false);
                            countTwo--;
                            codeTwo.remove(ballItemTwo.getBallNum());
                        } else {
                            ballItemTwo.setSelected(true);
                            countTwo++;
                            codeTwo.add(ballItemTwo.getBallNum());
                        }
                    }
                    setNumberMoney(countOne, countTwo, countThree);
                    itemOneAdapter.notifyDataSetChanged();
                    itemTwoAdapter.notifyDataSetChanged();
                    itemThreeAdapter.notifyDataSetChanged();
                } else {
                    if (ballItemTwo.isSelected()) {
                        ballItemTwo.setSelected(false);
                        countTwo--;
                        codeTwo.remove(ballItemTwo.getBallNum());
                    } else {
                        ballItemTwo.setSelected(true);
                        countTwo++;
                        codeTwo.add(ballItemTwo.getBallNum());
                    }
                    setNumberMoney(countOne, countTwo, countThree);
                    itemTwoAdapter.notifyDataSetChanged();
                }
                //胆拖投注
            } else {
                //如果当前球被选中则点击后取消选中
                if (ballItemTwo.isSelected()) {
                    ballItemTwo.setSelected(false);
                    countTwo--;
                    codeTwo.remove(ballItemTwo.getBallNum());
                } else {
                    boolean isCheck = false;
                    //点击的托码与胆码不匹配才能被选中
                    for (int i = 0; i < codeOne.size(); i++) {
                        if (ballItemTwo.getBallNum().equals(codeOne.get(i))) {
                            isCheck = true;
                        }
                    }
                    if (!isCheck) {
                        ballItemTwo.setSelected(true);
                        countTwo++;
                        codeTwo.add(ballItemTwo.getBallNum());
                    } else {
                        ToastUtil.showToast(this, "托码和胆码不能相同");
                    }
                }
                setNumberMoney(countOne, countTwo, countThree);
                itemTwoAdapter.notifyDataSetChanged();
            }
        } else if (parent.getId() == R.id.gridview_item_three) {
            if (codeOne.size() > 0) {
                for (int i = 0; i < codeOne.size(); i++) {
                    if (ballItemThree.getBallNum().equals(codeOne.get(i))) {
                        ballItemOne.setSelected(false);
                        countOne--;
                        codeOne.remove(ballItemOne.getBallNum());

                        ballItemThree.setSelected(true);
                        countThree++;
                        codeThree.add(ballItemThree.getBallNum());
                        break;
                    } else { //点击的item2和item1不匹配
                        if (i == codeOne.size() - 1) {
                            oneIsSelect = true;
                        }
                    }
                }
            } else { //item1中没有选中的
                oneIsSelect = true;
            }

            if (codeTwo.size() > 0) {
                for (int i = 0; i < codeTwo.size(); i++) {
                    if (ballItemThree.getBallNum().equals(codeTwo.get(i))) {
                        ballItemTwo.setSelected(false);
                        countTwo--;
                        codeTwo.remove(ballItemTwo.getBallNum());

                        ballItemThree.setSelected(true);
                        countThree++;
                        codeThree.add(ballItemThree.getBallNum());
                        break;
                    } else { //item2中没有和item3匹配的
                        if (i == codeTwo.size() - 1) {
                            twoIsSelect = true;
                        }
                    }
                }
            } else { //item2中没有选中
                twoIsSelect = true;
            }

            if (oneIsSelect && twoIsSelect) {

                if (ballItemThree.isSelected()) {
                    ballItemThree.setSelected(false);
                    countThree--;
                    codeThree.remove(ballItemThree.getBallNum());
                } else {
                    ballItemThree.setSelected(true);
                    countThree++;
                    codeThree.add(ballItemThree.getBallNum());
                }
            }

            setNumberMoney(countOne, countTwo, countThree);
            itemOneAdapter.notifyDataSetChanged();
            itemTwoAdapter.notifyDataSetChanged();
            itemThreeAdapter.notifyDataSetChanged();
        }

        //判断一下是否有选中的如果有的话，我们就变成清空；
        if (codeOne.size() > 0 || codeTwo.size() > 0 || codeThree.size() > 0) {
            mBottomBar.setLeftText("清空");
        } else {
            mBottomBar.setLeftText("机选");
        }
    }

    int num;
    int money;

    //设置投注金额
    private void setNumberMoney(int one, int two, int three) {
        num = 0;
        money = 0;
        Log.e(TAG, "one=" + one);
        Log.e(TAG, "two=" + two);
        Log.e(TAG, "three=" + three);
        int i = stringOfInt(modes);
        switch (i) {
            //普通前二直选
            case 8:
                num = one * two;
                money = num * 2;
                break;
            //普通前三直选
            case 9:
                num = one * two * three;
                money = num * 2;
                break;
            //胆拖任选二
            case 12:
                if (one == 1 && two >= 2) {

                    num = comb(two, 1);
                    money = num * 2;
                }
                break;
            //胆拖任选三
            case 13:
                //胆码大于0小于3 并且托码和胆码相加不少于4
                if ((one > 0 && one <= 2) && (one + two >= 4)) {

                    num = comb(two, 3 - one);
                    money = num * 2;
                }
                break;
            //胆拖任选四
            case 14:
                if ((one > 0 && one <= 3) && (one + two >= 5)) {
                    num = comb(two, 4 - one);
                    money = num * 2;
                }
                break;
            //胆拖任选五
            case 15:
                if ((one > 0 && one <= 4) && (one + two >= 6)) {
                    num = comb(two, 5 - one);
                    money = num * 2;
                }
                break;
            //胆拖任选六
            case 16:
                if ((one > 0 && one <= 5) && (one + two >= 7)) {
                    num = comb(two, 6 - one);
                    money = num * 2;
                }
                break;
            //胆拖任选七
            case 17:
                if ((one > 0 && one <= 6) && (one + two >= 8)) {
                    num = comb(two, 7 - one);
                    money = num * 2;
                }
                break;
            //胆拖前二组选
            case 18:
                if (one == 1 && two >= 2) {
                    num = two;
                    money = num * 2;
                }
                break;
            //胆拖前三组选
            case 19:
                if ((one > 0 && one <= 2) && (one + two >= 4)) {
                    num = comb(two, 3 - one);
                    money = num * 2;
                }
                break;
            default:
                num = BaseUtils.math2(one, number);
                money = num * 2;
                break;
        }
        if (num == 0) {
            tv_number_money.setVisibility(View.INVISIBLE);
        } else {
            tv_number_money.setVisibility(View.VISIBLE);
            tv_number_money.setText(num + "注 , " + money + "元");
        }
    }

    /**
     * 计算注数
     *
     * @param hasSelectTuoNumber  表示已经选择的托码的数量
     * @param needSelectTuoNumber 表示需要选择几个托码
     * @return 注数
     */
    public int comb(int hasSelectTuoNumber, int needSelectTuoNumber) {

        int a = 1;
        int b = 1;

        for (int i = 0; i < needSelectTuoNumber; i++) {
            a *= (hasSelectTuoNumber - i);
            b *= (i + 1);
        }
        return a / b;
    }


    private List<String> randomBallOne = new LinkedList<>();
    private List<String> randomBallTwo = new LinkedList<>();
    private List<String> randomBallThree = new LinkedList<>();


    /**
     * @category 机选
     */
    private void randomSelect() {
        vibrator.vibrate(500);
        clearBall();

        LinkedList<String> ball = MakeRandomUtil.getBall(11, 3);

        //获取机选随机数
        randomBallOne = MakeRandomUtil.getBall(11, number);
        for (int i = 0; i < itemOneAdapter.getCount(); i++) { //循环9次
            BallItem ballItem = (BallItem) itemOneAdapter.getItem(i);
            for (int j = 0; j < randomBallOne.size(); j++) { //循环随机数次
                //单选
                if (isRadio) {
                    if (ballItem.getBallNum().equals(ball.get(0))) {
                        ballItem.setSelected(true);
                        codeOne.add(ball.get(0));
                    }
                } else {
                    //非单选
                    if (ballItem.getBallNum().equals(randomBallOne.get(j))) {
                        ballItem.setSelected(true);
                        codeOne.add(randomBallOne.get(j));
                    }
                }
            }
        }


        if (itemCount == 2 || itemCount == 3) {
            //获取机选随机数
            randomBallTwo = MakeRandomUtil.getBall(11, number);
            for (int i = 0; i < itemTwoAdapter.getCount(); i++) {
                BallItem ballItem = (BallItem) itemTwoAdapter.getItem(i);
                for (int j = 0; j < randomBallTwo.size(); j++) {
                    if (isRadio) {
                        if (ballItem.getBallNum().equals(ball.get(1))) {
                            ballItem.setSelected(true);
                            codeTwo.add(ball.get(1));
                        }
                    } else {
                        if (ballItem.getBallNum().equals(randomBallTwo.get(j))) {
                            ballItem.setSelected(true);
                            codeTwo.add(randomBallTwo.get(j));
                        }
                    }
                }
            }
        }

        if (itemCount == 3) {
            //获取机选随机数
            randomBallThree = MakeRandomUtil.getBall(11, number);
            for (int i = 0; i < itemThreeAdapter.getCount(); i++) {
                BallItem ballItem = (BallItem) itemThreeAdapter.getItem(i);
                for (int j = 0; j < randomBallThree.size(); j++) {
                    if (isRadio) {
                        if (ballItem.getBallNum().equals(ball.get(2))) {
                            ballItem.setSelected(true);
                            codeThree.add(ball.get(2));
                        }
                    } else {
                        if (ballItem.getBallNum().equals(randomBallThree.get(j))) {
                            ballItem.setSelected(true);
                            codeThree.add(randomBallThree.get(j));
                        }
                    }
                }
            }
        }

        countTwo = randomBallTwo.size();
        countThree = randomBallThree.size();
        countOne = randomBallOne.size();
        setNumberMoney(countOne, countTwo, countThree);
        mBottomBar.setLeftText("清空");
    }

    //清空选中
    private void clearBall() {

        for (int i = 0; i < itemOneAdapter.getCount(); i++) {
            BallItem ballItemOne = (BallItem) itemOneAdapter.getItem(i);
            ballItemOne.setSelected(false);

            BallItem ballItemTwo = (BallItem) itemTwoAdapter.getItem(i);
            ballItemTwo.setSelected(false);

            BallItem ballItemThree = (BallItem) itemThreeAdapter.getItem(i);
            ballItemThree.setSelected(false);
        }
        codeOne.clear();
        codeTwo.clear();
        codeThree.clear();

        countOne = 0;
        countTwo = 0;
        countThree = 0;

        itemOneAdapter.notifyDataSetChanged();
        itemTwoAdapter.notifyDataSetChanged();
        itemThreeAdapter.notifyDataSetChanged();

        //tv_number_money.setText(Html.fromHtml("共0注，<font color=red>0</font>元"));
        tv_number_money.setVisibility(View.INVISIBLE);
        mBottomBar.setLeftText("机选");
    }


    boolean isTo; //是否跳转
    boolean restrict; //投注限制
    boolean duplexRestrict;//胆拖投注限制


    private void addSelector(boolean isTo) {
        formatBallNumber();

        if (isTo) {
            toActivity();
        }
        clearBall();
    }

    //整理选中的求数据
    private void formatBallNumber() {

        //不断生成销毁，数据由codeOnes决定
        StringBuffer strBuffer = null;
        StringBuffer strBufferTwo = null;
        StringBuffer strBufferThree = null;

        //普通投注
        if (currentPosition == 0) {
            if (modes.equals("前二直选")) {

                strBuffer = new StringBuffer();
                for (int j = 0; j < codeOne.size(); j++) {
                    if (j == codeOne.size() - 1) {
                        strBuffer.append(codeOne.get(j));
                    } else {
                        strBuffer.append(codeOne.get(j) + " ");
                    }
                }

                strBufferTwo = new StringBuffer();
                for (int j = 0; j < codeTwo.size(); j++) {
                    if (j == codeTwo.size() - 1) {
                        strBufferTwo.append(codeTwo.get(j));
                    } else {
                        strBufferTwo.append(codeTwo.get(j) + " ");
                    }
                }

                ballNumber.add(strBuffer.toString() + "|" + strBufferTwo.toString());
                //填充mode
                if (codeOne.size() > 2 || codeTwo.size() > 2) {
                    mode.add("前二直选 复式");
                } else {
                    mode.add("前二直选 单式");
                }
                //填充money
                num = codeOne.size() * codeTwo.size();
                money = num * 2;
                numMoney.add(num + "注" + " " + money + "元");
                totalMoney += money;
                totalNum += num;

            } else if (modes.equals("前三直选")) {

                strBuffer = new StringBuffer();
                for (int j = 0; j < codeOne.size(); j++) {
                    if (j == codeOne.size() - 1) {
                        strBuffer.append(codeOne.get(j));
                    } else {
                        strBuffer.append(codeOne.get(j) + " ");
                    }
                }

                strBufferTwo = new StringBuffer();
                for (int j = 0; j < codeTwo.size(); j++) {
                    if (j == codeTwo.size() - 1) {
                        strBufferTwo.append(codeTwo.get(j));
                    } else {
                        strBufferTwo.append(codeTwo.get(j) + " ");
                    }
                }

                strBufferThree = new StringBuffer();
                for (int j = 0; j < codeThree.size(); j++) {
                    if (j == codeThree.size() - 1) {
                        strBufferThree.append(codeThree.get(j));
                    } else {
                        strBufferThree.append(codeThree.get(j) + " ");
                    }
                }


                ballNumber.add(strBuffer.toString() + "|" + strBufferTwo.toString() + "|" + strBufferThree.toString());
                //填充mode
                if (codeOne.size() > 3 || codeTwo.size() > 3 || codeThree.size() > 3) {
                    mode.add("前三直选 复式");
                } else {
                    mode.add("前三直选 单式");
                }
                //填充money
                num = codeOne.size() * codeTwo.size() * codeThree.size();
                money = num * 2;
                numMoney.add(num + "注" + " " + money + "元");
                totalMoney += money;
                totalNum += num;
            } else {
                strBuffer = new StringBuffer();
                for (int j = 0; j < codeOne.size(); j++) {
                    if (j == codeOne.size() - 1) {
                        strBuffer.append(codeOne.get(j));
                    } else {
                        strBuffer.append(codeOne.get(j) + " ");
                    }
                }
                ballNumber.add(strBuffer.toString());
                num = BaseUtils.math2(codeOne.size(), number);
                money = num * 2;
                numMoney.add(num + "注" + " " + money + "元");
                totalMoney += money;
                totalNum += num;

                if (modes.equals("任选二")) {
                    if (codeOne.size() > 2) {
                        mode.add("任选二 复式");
                    } else {
                        mode.add("任选二 单式");
                    }
                } else if (modes.equals("任选三")) {
                    if (codeOne.size() > 3) {
                        mode.add("任选三 复式");
                    } else {
                        mode.add("任选三 单式");
                    }
                } else if (modes.equals("任选四")) {
                    if (codeOne.size() > 4) {
                        mode.add("任选四 复式");
                    } else {
                        mode.add("任选四 单式");
                    }
                } else if (modes.equals("任选五")) {
                    if (codeOne.size() > 5) {
                        mode.add("任选五 复式");
                    } else {
                        mode.add("任选五 单式");
                    }
                } else if (modes.equals("任选六")) {
                    if (codeOne.size() > 6) {
                        mode.add("任选六 复式");
                    } else {
                        mode.add("任选六 单式");
                    }
                } else if (modes.equals("任选七")) {
                    if (codeOne.size() > 7) {
                        mode.add("任选七 复式");
                    } else {
                        mode.add("任选七 单式");
                    }
                } else if (modes.equals("任选八")) {
                    if (codeOne.size() > 8) {
                        mode.add("任选八 复式");
                    } else {
                        mode.add("任选八 单式");
                    }
                } else if (modes.equals("前二组选")) {
                    if (codeOne.size() > 2) {
                        mode.add("前二组选 复式");
                    } else {
                        mode.add("前二组选 单式");
                    }
                } else if (modes.equals("前三组选")) {
                    if (codeOne.size() > 3) {
                        mode.add("前三组选 复式");
                    } else {
                        mode.add("前三组选 单式");
                    }
                } else if (modes.equals("前一直选")) {
                    if (codeOne.size() > 1) {
                        mode.add("前一直选 复式");
                    } else {
                        mode.add("前一直选 单式");
                    }
                }
            }
        } else {
            //胆码
            strBuffer = new StringBuffer();
            for (int j = 0; j < codeOne.size(); j++) {
                if (j == codeOne.size() - 1) {
                    strBuffer.append(codeOne.get(j));
                } else {
                    strBuffer.append(codeOne.get(j) + " ");
                }
            }
            //托码
            strBufferTwo = new StringBuffer();
            for (int j = 0; j < codeTwo.size(); j++) {
                if (j == codeTwo.size() - 1) {
                    strBufferTwo.append(codeTwo.get(j));
                } else {
                    strBufferTwo.append(codeTwo.get(j) + " ");
                }
            }

            ballNumber.add(strBuffer.toString() + "$" + strBufferTwo.toString());
            switch (stringOfInt(modes)) {
                case 12:
                    mode.add("任选二胆拖");
                    num = comb(codeTwo.size(), 2 - codeOne.size());
                    break;
                case 13:
                    mode.add("任选三胆拖");
                    num = comb(codeTwo.size(), 3 - codeOne.size());
                    break;
                case 14:
                    mode.add("任选四胆拖");
                    num = comb(codeTwo.size(), 4 - codeOne.size());
                    break;
                case 15:
                    mode.add("任选五胆拖");
                    num = comb(codeTwo.size(), 5 - codeOne.size());
                    break;
                case 16:
                    mode.add("任选六胆拖");
                    num = comb(codeTwo.size(), 6 - codeOne.size());
                    break;
                case 17:
                    mode.add("任选七胆拖");
                    num = comb(codeTwo.size(), 7 - codeOne.size());
                    break;
                case 18:
                    mode.add("前二组选胆拖");
                    num = comb(codeTwo.size(), 2 - codeOne.size());
                    break;
                case 19:
                    mode.add("前三组选胆拖");
                    num = comb(codeTwo.size(), 3 - codeOne.size());
                    break;
            }
            //填充money
            //num = codeTwo.size();
            money = num * 2;
            numMoney.add(num + "注" + " " + money + "元");
            totalMoney += money;
            totalNum += num;
        }
        mBottomBar.setBettingNumber(totalNum);
    }


    private class ViewHolder {
        TextView ball_num;
        TextView tv_missData;
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_ordinaryBetting:
                checked = 0;
                adapter.setData(ordinaryMode, ordinaryBonus);
                gv_selectBox.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                break;
            case R.id.rb_duplexBetting:
                adapter.setData(duplexMode, duplexBonus);
                gv_selectBox.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                checked = 1;
                break;
        }
    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        int sensorType = event.sensor.getType();
        float[] values = event.values;
        if (sensorType == Sensor.TYPE_ACCELEROMETER) {
            if ((Math.abs(values[0]) > 14 || Math.abs(values[1]) > 14 || Math.abs(values[2]) > 14)) {
                randomSelect();
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    private void toActivity() {

        for (int i = 0; i < ballNumber.size(); i++) {
            Log.e(TAG, "ballNumber=" + ballNumber.get(i));
            Log.e(TAG, "mode=" + mode.get(i));
        }

        Intent intent = new Intent(this, BetConfirmActivity.class);
        intent.putExtra("type", type); //彩种
        intent.putExtra("number", number); //个数
        intent.putExtra("totalMoney", totalMoney); //总金额
        intent.putExtra("totalNum", totalNum);
        intent.putStringArrayListExtra("ballNumber", ballNumber); //球
        intent.putStringArrayListExtra("mode", mode); //玩法及模式
        intent.putStringArrayListExtra("numMoney", numMoney); //注数及金额
        startActivityForResult(intent, 0);
    }

    private void selectBall() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null) return;
        switch (resultCode) {
            //继续选号
            case 0:
                ballNumber = data.getStringArrayListExtra("ballNumber");
                mode = data.getStringArrayListExtra("mode");
                numMoney = data.getStringArrayListExtra("numMoney");
                totalMoney = data.getIntExtra("totalMoney", totalMoney);
                totalNum = data.getIntExtra("totalNum", totalNum);

                mBottomBar.setBettingNumber(totalNum);
                break;
            //清除选号
            case 1:
                clearAllBetting();
                break;
            case -1:
                this.finish();
                break;
        }
    }


    @Override
    protected void onDestroy() {
        if (timer != null)
            timer.cancel(); // 取消
        if (manager != null) {
            manager.unregisterListener(this);
        }
        super.onDestroy();
    }


    int topHeight = 0;

    public int measureListViewWrongHeight(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return 0;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        topHeight = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        params.height = topHeight;
        listView.setLayoutParams(params);
        return totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    }


    public void showMissData() {
        //这里我们要拿到红色的和蓝色的adapter;

        itemOneAdapter.showMissData();
        itemTwoAdapter.showMissData();
        itemThreeAdapter.showMissData();

        //显示遗漏
        mTvMissData.setText("隐藏遗漏");
        PreferenceUtil.setIsShowMissing(this, true);
    }

    public void hideMissData() {

        itemOneAdapter.hideMissData();
        itemTwoAdapter.hideMissData();
        itemThreeAdapter.hideMissData();
        //隐藏遗漏
        mTvMissData.setText("显示遗漏");
        PreferenceUtil.setIsShowMissing(this, false);
    }

    private void clearAllBetting() {
        ballNumber.clear();
        mode.clear();
        numMoney.clear();
        totalMoney = 0;
        totalNum = 0;

        mBottomBar.setBettingNumber(totalNum);
    }


    @Override
    protected void sendRandom(Intent intent) {
        super.sendRandom(intent);
        //机选
        int num = intent.getIntExtra("num", 0);
        if (num > 0) {
            clearAllBetting();
            //开始机选
            for (int i = 0; i < num; i++) {
                randomSelect();
                addSelector(false);
            }
            intent.setAction(Constant_Result.EXECRANDOMLOTTERYNUM);
            intent.putExtra("type", type); //彩种
            intent.putExtra("number", number); //个数
            intent.putExtra("totalMoney", totalMoney); //总金额
            intent.putExtra("totalNum", totalNum);
            intent.putStringArrayListExtra("ballNumber", ballNumber); //球
            intent.putStringArrayListExtra("mode", mode); //玩法及模式
            intent.putStringArrayListExtra("numMoney", numMoney); //注数及金额
            sendBroadcast(intent);
        }
    }
}
